package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.Vote;
import com.ericom.service.GenericManager;

public class VoteAction extends BaseAction {

	private GenericManager<Vote, Long> voteManager;
	private List votes;
	private Vote vote;
	private Long id;

	public Vote getVote() {
		return vote;
	}

	public void setVote(Vote vote) {
		this.vote = vote;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setVoteManager(GenericManager<Vote, Long> voteManager) {
		this.voteManager = voteManager;
	}

	public String list() {
		votes = voteManager.getAll();
		return SUCCESS;
	}

	public List getVotes() {
		return votes;
	}

	public String delete() {
		voteManager.remove(vote.getId());
		this.saveMessage(getText("vote.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			vote = voteManager.get(id);
		} else {
			vote = new Vote();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (vote.getId() == null);
		vote = voteManager.save(vote);

		String key = (isNew) ? "vote.added" : "vote.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
